package GUI.TabPanel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import GUI.List.TableList;
import Struct.Tables;

public class DataBase extends JPanel {
	
	private TableList list = new TableList();
	private DataBasePanel panel = new DataBasePanel();
	private JPanel glue = new JPanel();
	private String Mac_ID = null;
	private ReadXML xml = new ReadXML();
	private ArrayList<Tables> table;
	
	public void Refresh(Object SelectObject) {
		
		if(SelectObject != null) {
			Mac_ID = (String) SelectObject;
			xml.setMac_ID(Mac_ID);
			table = xml.getTable();
			contextReFresh(table);
			setMac_ID(Mac_ID);
		}
	}
	
	public void contextReFresh(ArrayList<Tables> table) {
		list.reFresh(table);
		panel.setSrc(table);
	}
	
	public void setMac_ID(String Mac_ID) {
		panel.setMac_ID(Mac_ID);
	}
	
	public DataBase() {
		add(list);
		add(panel);
		
		list.setDataBase(panel);
		
		GroupLayout jPanel1Layout = new GroupLayout(this);
        this.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(list, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(glue, 35, 35, 35)
                .addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(glue, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(list, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
        );
        
	}
	

}

class ReadXML {
	
	private ArrayList<Tables> Schema = new ArrayList<Tables>();
	private String Mac_ID;
	private Tables table;
	private String SchemaName;
	private SAXBuilder xml = new SAXBuilder();
	
	public void setMac_ID(String Mac_ID) {
		this.Mac_ID = Mac_ID;
		File tmp = new File(Mac_ID + "/Data/Schema.xml");
		System.out.println("Mac_ID: " + Mac_ID + "/Data/" + tmp.getName());
		if(tmp.exists()) {
			try {
				this.Schema.clear();
				this.getData();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			this.Schema.clear();
		}
	}
	
	public ArrayList<Tables> getTable() {
		return Schema;
	}
	
	private void getTable(Tables table, List<Element> tables) {
		for (Element table2 : tables) {
			List<Element> columns = table2. getChildren("Column");
			if(columns.size() != 0) {
				table = new Tables();
				table.setName(SchemaName + "." + table2.getChildText("Name"));
				this.getColumn(table, columns);
			}
			Schema.add(table);
		}
	}
	
	private void getColumn(Tables table, List<Element> columns) {
		for (Element column : columns) {
			table.insertColumn(column.getChildText("name"), column.getChildText("typename"));
		}
	}
	
	private void getData() throws Exception {
		Schema.clear();
		Document doc = xml.build(this.Mac_ID + "/Data/Schema.xml");
	    Element root = doc.getRootElement();
	    List<Element> list = root.getChildren("Schema");
	    
	    for (Element schemas : list) {
	    	Element schema = schemas;
	    	List<Element> tables = schema.getChildren("Table");
	    	if(tables.size() != 0) {
	    		SchemaName = schema.getChildText("Name").replaceAll(" ", "").replaceAll("	","");
	    		this.getTable(table, tables);
	    	}
	    }
	}

	public String getMac_ID() {
		return Mac_ID;
	}
}

